package com.yaorange.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yaorange.dao.ModuleMapper;
import com.yaorange.entity.Module;
import com.yaorange.service.ModuleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class ModuleServiceImpl implements ModuleService {

    @Resource
    private ModuleMapper moduleMapper;

    //获取所有部门，回显要用
    @Override
    public List<Module> getParentList() {
        Example example = new Example(Module.class);
        Example.Criteria criteria = example.createCriteria();
        //只有父模块id为空的数据才是父模块（一级菜单）
        criteria.andIsNull("parentId");
        List<Module> modules = moduleMapper.selectByExample(example);
        return modules;
    }


    //查询当前模块信息(删除、更新会用)
    @Override
    public List<Module> getModule(Integer moduleId) {
        List<Module> modules = moduleMapper.selectByModuleId(moduleId);
        return modules;
    }


    //分页
    @Override
    public PageInfo<Module> getPageByCondition(Integer pageNum, Integer pageSize, String conditionModuleName) {
        PageHelper.startPage(pageNum,pageSize);
        //设置查询条件
        Example example = new Example(Module.class);
        Example.Criteria criteria = example.createCriteria();
        //搜索用户名
        if(conditionModuleName != null && !"".equals(conditionModuleName)){
            criteria.andLike("moduleName","%"+conditionModuleName+"%");
        }
        //不查询为1的信息（被删除了的）
        criteria.andNotEqualTo("moduleState",1);

        List<Module> modules = moduleMapper.selectByExample(example);
        return new PageInfo<>(modules);
    }



    //删除
    @Override
    public int updateModule(Module module) {
        int i = moduleMapper.updateByPrimaryKeySelective(module);
        return i;
    }

    @Override
    public List<Module> getList() {
        Example example = new Example(Module.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andNotEqualTo("moduleState",-1);
        return moduleMapper.selectByExample(example);
    }

//    @Override
//    public List<Module> selectAllByXML() {
//        return moduleMapper.selectAllByXML();
//    }


    //修改
}
